import { reactive, toRefs, watch, ref } from 'vue';
import { emitter } from '@/utils/bus.js'
import { appCurrentState } from '@/pinia/modules/app'

export const useTable = (api) => {
  const state = reactive({
    tableData: [],
    tableLoading: false,
    pagination: {
      page: 1,
      pageSize: 20,
      pageSizes: [10, 20, 30],
      total: 0,
      change: ({ page, limit }) => {
        //切换第几页时
        state.pagination.page = page;
        state.searchForm.pageNum = page;
        state.pagination.pageSize = limit;
        state.searchForm.pageSize = limit;
        getList(state.searchForm);
      },

    },
    searchForm: {
      pageNum: 1,
      pageSize: 20
    }
  });
  const search = params => {
    state.searchForm.pageNum = 1;
    state.pagination.page = 1;
    getList();
  };
  const getList = async params => {
    state.tableLoading = true;
    const res = await api(state.searchForm);
    state.tableData = res.data;
    state.pagination.total = res.total;
    state.tableLoading = false;
    // console.log(e);
  };

  return {
    ...toRefs(state),
    search,
    getList
  };
};

